﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

namespace SSHTunnel
{

    public class BlueSSH
    {
        private static string _url = @"http://bluessh.com/content/freessh/";
        private static AccountInfo _account = new AccountInfo();

        public static AccountInfo Account
        {
            get { return BlueSSH._account; }
            set { BlueSSH._account = value; }
        }

        public void Refresh()
        {
            AccountInfo account = new AccountInfo();
            using (WebClient client = new WebClient())
            {
                byte[] buffer = client.DownloadData(_url);
                string content = Encoding.UTF8.GetString(buffer);
                HtmlDocument document = new HtmlDocument();
                document.LoadHtml(content);

                HtmlNode root = document.DocumentNode;

                _account.Host = root.SelectSingleNode("//*[@id='contentHolder']/div/div/table/tr[2]/td").InnerText.Trim();
                _account.UserName = root.SelectSingleNode("//*[@id='contentHolder']/div/div/table/tr[4]/td").InnerText.Trim();

                string picURL = "https://bluessh.com" + root.SelectSingleNode("//*[@id='contentHolder']/div/div/table/tr[5]/td/img").Attributes["src"].Value;
                buffer = client.DownloadData(picURL);

                MemoryStream stream = new MemoryStream(buffer);
                Bitmap bitmap = new Bitmap(stream);

                _account.Img = bitmap;

                _account.Time = DateTime.Parse(root.SelectSingleNode("//*[@id='contentHolder']/div/div/table/tr[6]/td[2]").InnerText).AddMinutes(30);

                //string password = cracker.Read(bitmap);

                //if (!_account.Password.Equals(password))
                //{
                //    _account.Password = password;
                //    return true;
                //}
            }
        }
    }
}
